package com.lagou.rpc.provider;

import com.lagou.rpc.provider.server.RpcServer;
import com.lagou.rpc.provider.zk.ZkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@SpringBootApplication
public class ServerBootstrapApplication implements CommandLineRunner {

    @Autowired
    RpcServer rpcServer;

    public static void main(String[] args) {
        SpringApplication.run(ServerBootstrapApplication.class, args);
    }

    /**
     * 在这里启动两个服务端端口
     */
    @Override
    public void run(String... args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        String ip = "127.0.0.1";
        ZkUtil zkUtil = new ZkUtil();
        zkUtil.subscribeChildChanges();
        executorService.execute(() -> {
            int port = 8801;
            //将IP及端口信息注册到Zookeeper
            zkUtil.addNode(ip, port);
            rpcServer.startServer(ip, port);
        });

//        executorService.execute(() -> {
//            int port = 8818;
//            //将IP及端口信息注册到Zookeeper
//            ZkUtil zkUtil = new ZkUtil();
//            zkUtil.addNode(ip, port);
//            rpcServer.startServer(ip, port);
//        });
    }
}
